<template>
  <div class="table-all">
    <!-- 搜索区 -->
    <div class="app-container search-box">
      <SearchForm
        :form="searchData.form"
        :options="searchData.options"
        @searchUpdate="search"
        @resetSearchData="resetSearchData"
        @toLoadMethods="toLoadMethods"
      ></SearchForm>
    </div>
    <!-- 按钮区 -->
    <div class="app-container table-box">
      <div class="table-main">
        <!-- 表格 -->
        <v-table
          title="评优类型指标数据"
          v-if="tableMainHeight>0"
          :table-columns="tableColumns"
          :table-data="tableData"
          :loading="loading"
          :is-index="true"
          ref="table"
          :maxHeight="tableMainHeight">
          <template #topButtonList>
            <ButtonForm :buttonList="buttonList" @clickButton="clickButton"></ButtonForm>
          </template>
          <template #checkType="scope">
            {{getDictValue(scope.row.checkType, checkTypeOptions)}}
          </template>
          <template #operation="scope">
            <a class="table-btn-view table-operation"
               @click="handleDetail(scope.row)">{{$t('table.view')}}</a>
          </template>
        </v-table>
      </div>
      <div class="table-page">
        <!-- 页码 -->
        <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                    @pagination="search"/>
      </div>
    </div>
     <!--详情-->
    <detailDialog :dialog-visible="detailDialogShow"
                  @close="editClose"
                  :openType="detailOpenType"
                  :threeTargetNo="threeTargetNo"
                  :title="detailTitle"
                  :year="year"
                  :resultType="resultType"
                  :cityCheckTypeOptions = "checkTypeOptions"
                  :showHistoryChars="true"
    ></detailDialog>
    <!--指标树弹框-->
    <TargetDialog
      :dialog-visible="targetDialogShow"
      @close="targetEditClose"
      @success="targetEditSuccess"
      :title="targetTitle"
      :year="searchData.form.year"
    ></TargetDialog>
  </div>
</template>

<script>
  import { indicatorDiagnosisPage,
    indicatorDiagnosisExport
  } from '@/api/risk/cityWaringResult'
  import { getAreaList } from '@/api/system/area'
  import SearchForm from '@/components/searchForm/index'
  import ButtonForm from '@/components/ButtonForm/index'
  import VTable from '@/components/VTable/index'
  import Pagination from '@/components/Pagination/index'
  import detailDialog from './detail.vue'
  import TargetDialog from '@/views/xy/public/cityAnalysis/components/targetDialog'
  export default {
    name: "index",
    components: {
      VTable, Pagination, ButtonForm, detailDialog, SearchForm, TargetDialog
    },
    data() {
      return {
        searchData:{
          form:{
            year: new Date().getFullYear()+'',
            cityCode:"",
            checkType:"",
            targetName:"",
            targetCode:"",
            targetType:"",
          },
          options:[
            {
              title:'年份',
              key: "year",
              type: "dateYear",
            },
            {
              title:'城市',
              key: "cityCode",
              type: "select",
              multiple:false,
              option: {
                data: this.cityList,
                props: {
                  key: "id", // 指定key对应data的字段
                  label: "areaName", // 指定label对应data的字段
                  value: "areaCode", // 指定value对应data的字段
                },
              },
            },
            {
              title:'体检分类',
              key: "checkType",
              type: "select",
              multiple:false,
              option: {
                data: this.checkTypeOptions,
                props: {
                  key: "id", // 指定key对应data的字段
                  label: "label", // 指定label对应data的字段
                  value: "value", // 指定value对应data的字段
                },
              },
            },
            {
              title:'指标分类',
              key: "targetName",
              type: "clickText",
              event:"openTreeDialog"
            }
          ]
        },
        tableColumns:[
          {
            prop: 'threeTargetName',
            label: '指标名称',
            minWidth: '200',
            slotname: 'threeTargetName',
            options: {}
          },
          {
            prop: 'checkType',
            label: '体检分类',
            minWidth: '150',
            slotname: 'checkType',
            options: {}
          },
          {
            prop: 'oneTargetName',
            label: '指标大类',
            minWidth: '150',
            slotname: 'oneTargetName',
            options: {}
          },
          {
            prop: 'twoTargetName',
            label: '指标细类',
            minWidth: '150',
            slotname: 'twoTargetName',
            options: {}
          },
          {
            prop: 'year',
            label: '年份',
            minWidth: '120',
            slotname: 'year',
            options: {}
          },
          {
            prop: 'youXiu',
            label: '优秀',
            minWidth: '120',
            slotname: 'youXiu',
            options: {}
          },
          {
            prop: 'liangHao',
            label: '良好',
            minWidth: '120',
            slotname: 'liangHao',
            options: {}
          },
          {
            prop: 'yiBan',
            label: '一般',
            minWidth: '120',
            slotname: 'yiBan',
            options: {}
          },
          {
            prop: 'yuJing',
            label: '预警',
            minWidth: '120',
            slotname: 'yuJing',
            options: {}
          },
          {
            prop: 'countResult',
            label: '指标城市体检结果',
            minWidth: '220',
            slotname: 'countResult',
            options: {}
          },
          {
            prop: 'operation',
            label: '操作',
            width: '200',
            align: 'center',
            slotname: 'operation',
            overflow: false,
            fixed: 'right'
          }
        ],
        tableData:[],
        selection: [],
        loading:false,
        total:1,
        pageNum:1,
        pageSize:10,
        detailOpenType:"add",
        detailDialogShow:false,
        detailTitle:"新增",
        year:"",
        threeTargetNo:"",
        resultType:"",
        buttonList:[
          {name:'导出数据',methods:'handleExport', type:'primary',  hasPremission:["indicatorDiagnosis:export"]}
        ],
        tableMainHeight: 0,
        // 字典
        checkTypeOptions:[],
        cityList:[],
        targetDialogShow:false,
        targetTitle:"选择体检指标",
      }
    },
    watch:{
      'cityList':{
        handler(newVal, oldVal){
          if(newVal){
            this.searchData.options.forEach(t=>{
              if(t.key == 'cityCode'){
                t.option.data = newVal
              }
            })
          }
        }
      },
      'checkTypeOptions':{
        handler(newVal, oldVal){
          if(newVal){
            this.searchData.options.forEach(t=>{
              if(t.key == 'checkType'){
                if(newVal){
                  t.option.data = newVal.filter(t=>t.value!=3)
                }
              }
            })
          }
        }
      },
    },
    methods: {
      targetEditClose(){
        this.targetDialogShow =false
      },
      targetEditSuccess(data){
        console.log("获取值",data)
        this.searchData.form.targetName = data.targetName
        this.searchData.form.targetCode = data.targetCode,
        this.searchData.form.targetType = data.targetType
        this.targetEditClose()
      },
      toLoadMethods(methods) {
        this[methods]()
      },
      openTreeDialog(){
        console.log("打开弹框===========>")
        this.targetDialogShow = true
      },
      editClose(){
        this.detailDialogShow = false
      },
      handleDetail(row){
        this.threeTargetNo = row.threeTargetNo
        this.year = this.searchData.form.year
        this.resultType= 'rate'
        this.detailOpenType = "detail"
        this.detailTitle="详情"
        this.detailDialogShow = true
      },
      getDictValue(value,option){
        let item = option.find(t=> t.value == value)
        if(item!=undefined){
          return item.label
        }
        return value
      },
      search(){
        this.fetch({
          ...this.searchData.form,
        })
      },
      fetch(params={}){
        params.pageSize = this.pageSize
        params.pageNum = this.pageNum
        params.targetName = ''
        this.loading = true
        params.resultType = "rate"
        indicatorDiagnosisPage(params).then(res=>{
          if(res.code==200){
            this.total = res.data.total
            this.tableData = res.data.rows
          }else{
            this.$message({
              message: this.$t('tips.systemException'),
              type: 'error'
            })
          }
        }).finally(final=>{
          this.loading = false
        })
      },
      // 重置搜索条件
      resetSearchData(){
        this.searchData.form = {
          year: new Date().getFullYear()+'',
          cityCode:"",
          checkType:"",
          targetName:"",
          targetCode:"",
          targetType:"",
        }
      },
      // 触发按钮区域按钮方法
      clickButton(methods){
        this[methods]()
      },
      handleExport(){
        let params = {
          ...this.searchData.form,
          resultType :"rate"
        }
        params.targetName = ''
        indicatorDiagnosisExport(params,`评优类型指标信息_${new Date().getTime()}.xlsx`);
      },
      closeDialog(){
        this.detailDialogShow = false
      }
    },
    activated(){
      this.fetch({
        ...this.searchData.form,
      })
    },
    created(){
      // 获取字典值
      this.checkTypeOptions = []
      this.$commonJs.filterDataDict("cityCheckType",this.checkTypeOptions)
      getAreaList({parentCode:"440000000000"}).then(res=>{
        if(res.code==200){
          this.cityList = res.data
        }
      })
      this.fetch({
        ...this.searchData.form,
      })
    },
    mounted(){
      this.tableMainHeight = document.getElementsByClassName('table-main')[0].clientHeight - 44 //减去顶部按钮列表的高度
      console.log("this.tableMainHeight",this.tableMainHeight)
    }
  }
</script>

<style  lang="scss" scoped>
  .table-all {
    display: flex;
    flex-direction: column;
    height: 100%;

    .search-box {
      padding-bottom: 6px;
    }

    .table-box {
      padding-top: 10px;
      flex: 1;
      display: flex;
      flex-direction: column;

      .table-main {
        flex: 1;
        overflow: auto;
      }

      .table-page {
        // padding-top: 10 px;
        // padding-bottom: 10 px;
      }

      ::v-deep .pagination-container {
        text-align: right;
      }

    }
  }
</style>
